package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.SalesOrder;
import cn.stylefeng.guns.modular.system.mapper.SalesOrderMapper;
import cn.stylefeng.guns.modular.system.model.params.SalesOrderParam;
import cn.stylefeng.guns.modular.system.model.result.SalesOrderResult;
import cn.stylefeng.guns.modular.system.model.result.resultVO.SaleOrderVO;
import cn.stylefeng.guns.modular.system.service.SalesOrderService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 销售订单 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-07-04
 */
@Service
public class SalesOrderServiceImpl extends ServiceImpl<SalesOrderMapper, SalesOrder> implements SalesOrderService {

    @Override
    public void add(SalesOrderParam param) {
        SalesOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SalesOrderParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(SalesOrderParam param) {
        SalesOrder oldEntity = getOldEntity(param);
        SalesOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SalesOrderResult findBySpec(SalesOrderParam param) {
        return null;
    }

    @Override
    public List<SalesOrderResult> findListBySpec(SalesOrderParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SalesOrderParam param, Long userId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param, userId);
        List<SaleOrderVO> saleOrderVOS = page.getRecords();
        saleOrderVOS.forEach((s) -> {
            StringBuilder areas = new StringBuilder();
            areas.append(s.getProvinceName()).append(s.getCityName()).append(s.getAreaName()).append(s.getSalesAddr());
            s.setReciStr(areas.toString());
        });
        page.setRecords(saleOrderVOS);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public SalesOrder getByProcessInstanceId(String processInstanceId) {
        return this.baseMapper.getByProcessInstanceId(processInstanceId);
    }

    private Serializable getKey(SalesOrderParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SalesOrder getOldEntity(SalesOrderParam param) {
        return this.getById(getKey(param));
    }

    private SalesOrder getEntity(SalesOrderParam param) {
        SalesOrder entity = new SalesOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
